/**
 * 
 */
package com.bms.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;

import com.bms.Logic;
import com.bms.abstracts.RESTController;
import com.bms.domain.User;

/**
 * @author Administrator
 *
 */
public class IndexController extends RESTController {
	
	private Logic logic;
	
	public void setLogic(Logic logic) {
		this.logic = logic;
	}

	/* (non-Javadoc)
	 * @see com.bms.abstracts.RESTController#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView doGet(HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("check",1);
		return new ModelAndView("index");
	}

	/* (non-Javadoc)
	 * @see com.bms.abstracts.RESTController#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView doPost(HttpServletRequest request,
			HttpServletResponse response) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		boolean u = logic.auth(user);
		if(u){
			HttpSession session = request.getSession();
			User us = logic.getUser(username);
			session.setAttribute("user",us);
			try {
				response.sendRedirect("/BMS/console.servlet");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			request.setAttribute("check",0);
			return new ModelAndView("index");
		}
		return null;
	}

}
